package hao.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import hao.itheima.entity.PageBean;
import hao.itheima.entity.Student;
import hao.itheima.mapper.StudentMapper;
import hao.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public PageBean page(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        //1、进行分页设置
        PageHelper.startPage(page, pageSize);
        //2、调用Mapper层进行数据数据查询, 并并返回数据
        List<Student> dataList = studentMapper.list(name, degree, clazzId);
        //3、强转成分页对象
        Page<Student> p = (Page<Student>) dataList;
        //4、创建对象并返回数据
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        //1、直接调用Mapper进行删除
        //Batch 批 一批的意思
        studentMapper.deleteBatch(ids);
    }

    /**
     * 新增学员
     *
     * @param student
     */
    @Override
    public void save(Student student) {
        //1、补齐基础属性
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        //2、调用Mapper进行插入数据
        studentMapper.insert(student);
    }

    /**
     * 查询回显,的业务逻辑层
     *
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id) {
        //1、直接调用Mapper进行查询并返回数据
        return studentMapper.getById(id);
    }

    /**
     * 修改学员的逻辑处理层
     *
     * @param student
     */
    @Override
    public void update(Student student) {
        //1、补齐基本属性
        student.setUpdateTime(LocalDateTime.now());
        //2、调用Mapper层进行修改数据
        studentMapper.update(student);
    }

    /**
     * 违纪处理逻辑
     *
     * @param id
     * @param score
     */
    @Override
    public void violationScore(Integer id, Integer score) {
        //1.直接调用Mapper层进行处理
        studentMapper.violationScore(id, score);
    }
}
